
<?php
/*
 * 		author GuoLong
 * 		2014/12/04
 * 		email:guolong@howdo.cc
 */
class PersonController extends Controller {

	public function __construct() {
		parent::__construct ();
		$this->view->web_host = $this->getRequest ()->hostUrl;
		$this->view->web_app_url = $this->getRequest ()->hostUrl . "/clientapi.php";
	}
	//test
	public function Index(){
		$this->view->display("index.htm");
	}
	//获取成长记录统计数
// 	http://localhost/xdfServer/clientapi1.php/person/Getgrowth
	public function Getgrowth(){
		$msg="";
		$data=null;
		$userinfo = $this->getData ( 'userinfo' );
		$user_id=$userinfo ['id'];
		//test
//  		$user_id=42;
 		$log=new log();
 		//需要从用户日志中获取
		$video_total=$log->getTotalVideo($user_id);
		$video_done=$log->getMyTotalVideo($user_id);
		
		$item=new item();
		$item_feedback=new item_feedback();
		//题目统计
		$homework=new homework();
		$item_cannot=$item_feedback->getCannotItemTotal($user_id);
		$item_done=$item_feedback->getDoneItemTotal($user_id);
		$item_total=$homework->getStudentItemTotal($user_id);
		
		$homework_done=$item_feedback->getDoneHomeworkTotal($user_id);
		$homework_total=$item_feedback->getMyHomeworkTotal($user_id);
		//问题统计
		$qa=new questionoranswer();
		$question_done=$qa->getMyQANum($user_id);
		$question_total=$qa->getQANum();
		
		
		//视频统计
		$data["video_done"]=($video_done)?$video_done:0;
		$data["video_total"]=($video_total)?$video_total:0;
		
		$data["homework_done"]=($homework_done)?$homework_done:0;
		$data["homework_total"]=($homework_total)?$homework_total:0;
		
		$data["item_done"]=($item_done)?$item_done:0;
		$data["item_total"]=($item_total)?$item_total:0;
		$data["item_cannot"]=($item_cannot)?$item_cannot:0;
		
		$data["question_done"]=($question_done)?$question_done:0;
		$data["question_total"]=($question_total)?$question_total:0;
		if(1){
			
		$this->view->setMsg("获取收藏成功！");
		$this->view->setState(1);
		}else{
			$this->view->setMsg($user_id."获取收藏失败！");
			$this->view->setState(0);
		}
		$this->view->setData($data);
		$this->view->display("json");
	}
	//获取我不会的题目列表
	/*
	 * subject_id,start_datetime,end_datetime
	 */
// 	http://localhost/xdfServer/clientapi1.php/person/GetCannotItemList/pagesize/10/timestamp/0/action/more
	public function GetCannotItemList(){
		$msg="";
		$data=null;
		$subject_id=$this->getRequest()->get("subject_id");
		$start_datetime=$this->getRequest()->get("start_datetime");
		$end_datetime=$this->getRequest()->get("end_datetime");
		
		$pagesize=$this->getRequest()->get("pagesize");
		$timestamp=$this->getRequest()->get("timestamp");
		$action=$this->getRequest()->get("action");
		$pagesize.$timestamp.$action.$subject_id.$start_datetime.$end_datetime.

		$subject_id=($subject_id)?$subject_id:"0";
		$start_datetime=($start_datetime)?$start_datetime:"0";
		$end_datetime=($end_datetime)?$end_datetime:"0";
		$pagesize=($pagesize)?$pagesize:"10";
		$timestamp=($timestamp)?$timestamp:"10000";
		$action=($action)?$action:"pre";
		
		$userinfo = $this->getData ( 'userinfo' );
		$user_id=$userinfo ['id'];
		//$user_id = 20;
		$item_feedback=new item_feedback();
		$list=$item_feedback->getCannotItemList($user_id,$timestamp,$pagesize,$action,$subject_id,$start_datetime,$end_datetime);
		
 		if($list){
 			$video=new video();
 			foreach($list as $key=>$val){
 				$video_info=$video->stuGetVideoByItem($user_id, $list[$key]["item_id"]);
 				$list[$key]["video_id"]=$video_info["video_id"];
 			}
 			$totalcount=count($list);
 			$data=array("list"=>$list,"totalcount"=>$totalcount);
 			$this->view->setMsg("获取不会题目成功！");
 			$this->view->setState(1);
 		}else{		
 			/* $this->view->setMsg($user_id.$pagesize.$timestamp.$action.$subject_id.$start_datetime.$end_datetime."暂无数据！"); */
 			$this->view->setMsg("没有更多了哟！");
 			$this->view->setState(0);
 		}
 		$this->view->setData($data);
 		$this->view->display("json");
	}
	//获取收藏题目列表
// 	http://localhost/xdfServer/clientapi1.php/person/getcollectionItemList/pagesize/10/timestamp/0/action/more
	public function GetCollectionItemList(){
		$msg="";
		$data=null;
		$subject_id=$this->getRequest()->get("subject_id");
		$start_datetime=$this->getRequest()->get("start_datetime");
		$end_datetime=$this->getRequest()->get("end_datetime");
		
		$pagesize=$this->getRequest()->get("pagesize");
		$timestamp=$this->getRequest()->get("timestamp");
		$action=$this->getRequest()->get("action");
		
		$userinfo = $this->getData ( 'userinfo' );
		$user_id=$userinfo ['id'];
		

		$subject_id=($subject_id)?$subject_id:"0";
		$start_datetime=($start_datetime)?$start_datetime:"0";
		$end_datetime=($end_datetime)?$end_datetime:"0";
		$pagesize=($pagesize)?$pagesize:"10";
		$timestamp=($timestamp)?$timestamp:"1000";
		$action=($action)?$action:"pre";
		
		//$user_id = 20;
		$itemcollection=new itemcollection();
		$list=$itemcollection->getCollectionItemList($user_id,$timestamp,$pagesize,$action,$subject_id,$start_datetime,$end_datetime);
 		if($list){
 			
 			$video=new video();
 			foreach($list as $key=>$val){
 				$video_info=$video->stuGetVideoByItem($user_id, $list[$key]["item_id"]);
 				$list[$key]["video_id"]=$video_info["video_id"];
 			}
			$totalcount=count($list);
 			$data=array("list"=>$list,"totalcount"=>$totalcount);
 			$this->view->setMsg("获取收藏成功！");
 			$this->view->setState(1);
 		}else{
 			//$this->view->setMsg($subject_id."-".$user_id."暂无数据！");
 			$this->view->setMsg("没有更多了哟");
 			$this->view->setState(0);
 		}
 		$this->view->setData($data);
 		$this->view->display("json");
	}
	
// 	http://localhost/xdfServer/clientapi1.php/person/GetSysMessage/pagesize/10/timestamp/100/action/more
	public function GetSysMessageList(){
		$msg="";
		$userinfo = $this->getData ( 'userinfo' );
		$user_id=$userinfo ['id'];
		$pagesize=$this->getRequest()->get("pagesize");
		$timestamp=$this->getRequest()->get("timestamp");
		$action=$this->getRequest()->get("action");
		
		$message=new message();
		$list=$message->getReceivedMessageList($user_id, $timestamp, $pagesize, $action);
		
		
		if($list){
			$user=new user();
			foreach ($list as $key => $val){
				switch ($list[$key]["type"]){
					case MessageCodeEnum::MESSAGE_SYS_NEW_VISION:
						break;
					case MessageCodeEnum::MESSAGE_SYS_REPLAY_FEEDBACK:
						break;
					case MessageCodeEnum::MESSAGE_SYS_OFFLINE:
						break;
					case 11:
						break;
					case 12:
						break;
					case 13:
						break;
					case MessageCodeEnum::MESSAGE_STU_NEW_HOMEWORK:
						$period_id=$list[$key]["argv"];
						//获取新作业的必要参数并返回
						$homework=new homework();
						$homework_info=$homework->getHomeworkByPeriod($period_id);
						$list[$key]["homework_id"]=$homework_info["homework_id"];
						$list[$key]["need_feedback"]=$homework_info["need_feedback"];
						break;
					case MessageCodeEnum::MESSAGE_STU_REPLY_ITEM:
						break;
					case MessageCodeEnum::MESSAGE_STU_REPLY_PERIOD:
						break;
				}
				if((int)$list[$key]["sender"]<4)
					$list[$key]["sender_name"]="好度科技";
				else{
					$senderInfo=$user->getInfoByid($list[$key]["sender"]);
					$list[$key]["sender_name"]=$senderInfo["name"];
				}
			}
			$totalcount=count($list);
			$data=array("list"=>$list,"totalcount"=>$totalcount);
			$this->view->setMsg("获取系统消息成功！");
			$this->view->setState(1);
		}else{
			$data=null;
			$this->view->setMsg("暂无系统消息");
			$this->view->setState(0);
		}
		$this->view->setData($data);
		$this->view->display("json");
		
	}
	//变更一条信息的状态 type: -1:隐藏（删除） 1:已查看
	public function CheckMessage(){
		$userinfo = $this->getData ( 'userinfo' );
		$user_id = $userinfo ['id'];
		$id = $this->getRequest()->get('message_id');
		$type = $this->getRequest()->get('type');
		$message = new message();
		if($message->getMyMessageById($user_id, $id)){
			$result = $message->checkMessage($id, $type);
			if($result){
				$this->view->setState ( "1" );
				$this->view->setMsg ( "操作成功" );
			}else{
				$this->view->setState ( "0" );
				$this->view->setMsg ( "操作失败" );
			}
		}else{
				$this->view->setState ( "0" );
				$this->view->setMsg ( "参数错误" );
		}
		$this->view->setData ((object)null );
		$this->view->display ( "json" );
	}
	//变更一条信息的状态 type: -1:隐藏（删除） 1:已查看
	public function CheckAllMessage(){
		$userinfo = $this->getData ( 'userinfo' );
		$user_id = $userinfo ['id'];
		$type = $this->getRequest()->get('type');
		$message = new message();
		if($message->getReceivedMessageList($user_id, 0, 1, "pre")){
			$result = $message->checkAllMessage($user_id, $type);
			if($result){
				$this->view->setState ( "1" );
				$this->view->setMsg ( "操作成功" );
			}else{
				$this->view->setState ( "0" );
				$this->view->setMsg ( "操作失败" );
			}
		}else{
			$this->view->setState ( "0" );
			$this->view->setMsg ( "暂无消息" );
		}
		$this->view->setData ((object)null );
		$this->view->display ( "json" );
	}
}
